using System;
using System.Collections.Generic;

using Blog.Net.Common.DataObjects;
using Blog.Net.Business.BusinessObjects;

using NHibernate;
using NHibernate.Criterion;
using CureConsulting.DataAccess;

namespace Blog.Net.DataAccess.DataControls
{
	public partial class BlogCategoryDataControl : IDataControl<BlogCategoryBO>
	{
		#region FieldNames
		
		/// <summary>
        /// Struct to hold field names
        /// </summary>
		public struct FieldNames
        {
			/// <summary>
        	/// HQL Property Name for Id
        	/// </summary>
			public const string Id = "Id";
			/// <summary>
        	/// HQL Property Name for Name
        	/// </summary>
			public const string Name = "Name";
			/// <summary>
        	/// HQL Property Name for Description
        	/// </summary>
			public const string Description = "Description";
			/// <summary>
        	/// HQL Property Name for BlogEntries
        	/// </summary>
			public const string BlogEntries = "BlogEntries";
		}
		
		#endregion
		
		#region Private Members
		
		private static BlogCategoryDataControl blogCategoryDataControl;
		private static object lockBlogCategoryDataControl = new object();
		private static NHibernateDataControl<BlogCategory> dataControl = new NHibernateDataControl<BlogCategory>();
		
		#endregion
		
		#region Constructors
		
		/// <summary>
		/// Constructor for blog_category.
		/// </summary>
		public BlogCategoryDataControl(){}
		
		#endregion
		
		#region Private Methods

        private static IList<BlogCategoryBO> ConvertPOCOListToBOList(IList<BlogCategory> blogCategories)
        {
            List<BlogCategoryBO> retVal = new List<BlogCategoryBO>();

            if (blogCategories != null)
            {
                foreach (BlogCategory blogCategory in blogCategories)
                {
                    retVal.Add(new BlogCategoryBO(blogCategory));
                }
            }

            return retVal;
        }

        #endregion
		
		#region Public Properties

        /// <summary>
        ///Singleton Instance
        /// </summary>
        public static BlogCategoryDataControl Instance
        {
            get
            {
                lock (lockBlogCategoryDataControl)
                {
                    if(blogCategoryDataControl == null)
                    {
                        blogCategoryDataControl = new BlogCategoryDataControl();
                    }
                }

                return blogCategoryDataControl;
            }
        }

        #endregion
		
		#region CRUD

        /// <summary>
        /// Function to Save a BlogCategory
        /// </summary>
        /// <param name="blogCategory"></param>
        /// <returns></returns>
        public int? Save(BlogCategoryBO blogCategory)
        {
            int? retVal = dataControl.Save(blogCategory.POCO);
            if (retVal.HasValue)
            	dataControl.Refresh(blogCategory.POCO);
            return retVal;
        }

        /// <summary>
        /// Function to Save a list of blog_categories
        /// </summary>
        /// <param name="blogCategories"></param>
        /// <returns></returns>
        public bool Save(IList<BlogCategoryBO> blogCategories)
        {
            List<BlogCategory> itemsToSave = new List<BlogCategory>();

            foreach (BlogCategoryBO blogCategory in blogCategories)
            {
                itemsToSave.Add(blogCategory.POCO);
            }

            return dataControl.Save(itemsToSave);
        }

        /// <summary>
        /// Function to delete a BlogCategory
        /// </summary>
        /// <param name="blogCategory"></param>
        /// <returns></returns>
        public bool Delete(BlogCategoryBO blogCategory)
        {
            return dataControl.Delete(blogCategory.POCO);
        }

        /// <summary>
        /// Function to Delete a list of blog_categories
        /// </summary>
        /// <param name="blogCategories"></param>
        /// <returns></returns>
        public bool Delete(IList<BlogCategoryBO> blogCategories)
        {
            List<BlogCategory> itemsToDelete = new List<BlogCategory>();

            foreach (BlogCategoryBO blogCategory in blogCategories)
            {
                itemsToDelete.Add(blogCategory.POCO);
            }

            return dataControl.Delete(itemsToDelete);
        }		
		
        #endregion
		
		#region Queries
		
		/// <summary>
        /// Get BlogCategory by ID
        /// </summary>
        /// <param name="id">ID of BlogCategory</param>
        /// <returns>The BlogCategory</returns>
        public BlogCategoryBO GetById(int? id)
        {
			BlogCategory retVal = dataControl.GetObjectById(id);
			
			if(retVal != null)
				return new BlogCategoryBO(retVal);
			
			return null;
		}
		
		/// <summary>
        /// Return list of all blog_categories
        /// </summary>
        /// <returns></returns>
        public IList<BlogCategoryBO> GetAll()
        {
			IList<BlogCategory> retVal = dataControl.GetQueryResult(dataControl.GetCriteria());

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
         
			return new List<BlogCategoryBO>();
        }
		
		/// <summary>
        /// Return list of all blog_categories
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        public IList<BlogCategoryBO> GetAll(int firstResult, int maxResults)
        {
			IList<BlogCategory> retVal = dataControl.GetQueryResult(dataControl.GetCriteria(), firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
            
			return new List<BlogCategoryBO>();
        }
		
		/// <summary>
        /// Return list of all blog_categories
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
		/// <param name="sortExpression"></param>
        /// <returns></returns>
        public IList<BlogCategoryBO> GetAll(int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();

            if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new NHibernate.Criterion.Order(sort[0],ascending));
            }
			
			IList<BlogCategory> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
        
			return new List<BlogCategoryBO>();
        }
		
		/// <summary>
        /// Return count of all blog_categories
        /// </summary>
        /// <returns></returns>
        public int GetCountOfAll()
        {			
			int result = (int)dataControl.GetCriteria().SetProjection(Projections.RowCount()).UniqueResult();
			return result;
        }
		
		/// <summary>
        /// Query to get blog_categories by Name
        /// </summary>
		/// <param name="name">Name of the BlogCategory</param>
		/// <param name="firstResult"></param>
        /// <returns>The blog_categories</returns>
        public BlogCategoryBO GetByName(string name)
        {
			ICriteria criteria = dataControl.GetCriteria();
            criteria.Add(Expression.Eq(FieldNames.Name, name));
						
			BlogCategory retVal = dataControl.GetUniqueQueryResult(criteria);
			
			if(retVal != null)
				return new BlogCategoryBO(retVal);
				
			return null;	
        }
		
		#endregion
	}
}